package model.core;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import model.area_effect.MapAreaOfEffect;
import model.area_effect.Trap;
import model.entity.Entity;
import model.item.InventoryItem;
import model.map.MapPoint;

public class PlacementParser {
	private BufferedReader in;
	private LineParser lineParser;
	
	public PlacementParser(String filepath){
		try {
			in = new BufferedReader(new FileReader(filepath));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.lineParser = new LineParser();
	}
	
	public HashMap<MapPoint, List<InventoryItem>> parseItemsMap() throws IOException{
		HashMap<MapPoint, List<InventoryItem>> items = new HashMap<MapPoint, List<InventoryItem>>();
		
		String line = new String();
		while (in.ready()){
			line = in.readLine().trim();
			//System.out.println(line);
			if (line.equals("END") || line.equals("AOE")){
				break;
			}
			String id = in.readLine().trim();
			if (line.equals("Item")){
				ArrayList<String> tokens = lineParser.parseStringOnSemicolon(id);
				ArrayList<String> p = lineParser.parseStringOnCommaAndRemoveParen(tokens.remove(0));
				MapPoint point = new MapPoint(Integer.parseInt(p.get(0)), Integer.parseInt(p.get(1)));
				
				ArrayList<InventoryItem> its = new ArrayList<InventoryItem>();
				for (String name : tokens){
					its.add(ItemsLibrary.getInstance().getItem(name));
				}
				
				items.put(point, its);
			}
		}
		return items;
	}
	public HashMap<MapPoint, List<MapAreaOfEffect>> parseAoEMap() throws IOException{
		HashMap<MapPoint, List<MapAreaOfEffect>> aoes = new HashMap<MapPoint, List<MapAreaOfEffect>>();
		
		String line = new String();
		while (in.ready()){
			line = in.readLine().trim();
			//System.out.println(line);
			if (line.equals("END") || line.equals("Entity")){
				break;
			}
			String id = in.readLine().trim();
			if (line.equals("AOE")){
				ArrayList<String> tokens = lineParser.parseStringOnSemicolon(id);
				ArrayList<String> p = lineParser.parseStringOnCommaAndRemoveParen(tokens.remove(0));
				MapPoint point = new MapPoint(Integer.parseInt(p.get(0)), Integer.parseInt(p.get(1)));
				
				ArrayList<MapAreaOfEffect> its = new ArrayList<MapAreaOfEffect>();
				for (String name : tokens){
					its.add(MapAoEsLibrary.getInstance().getMapAreaOfEffect(name));
				}
				
				aoes.put(point, its);
			}
		}
		return aoes;
	}
	public HashMap<MapPoint, List<Trap>> parseTrapMap() throws IOException{
		HashMap<MapPoint, List<Trap>> traps = new HashMap<MapPoint, List<Trap>>();
		
		String line = new String();
		while (in.ready()){
			line = in.readLine().trim();
			//System.out.println(line);
			if (line.equals("END") || line.equals("Entity")){
				break;
			}
			String id = in.readLine().trim();
			if (line.equals("Trap")){
				ArrayList<String> tokens = lineParser.parseStringOnSemicolon(id);
				ArrayList<String> p = lineParser.parseStringOnCommaAndRemoveParen(tokens.remove(0));
				MapPoint point = new MapPoint(Integer.parseInt(p.get(0)), Integer.parseInt(p.get(1)));
				
				ArrayList<Trap> its = new ArrayList<Trap>();
				for (String name : tokens){
					its.add(TrapsLibrary.getInstance().getTrap(name));
				}
				
				traps.put(point, its);
			}
		}
		return traps;
	}
	public HashMap<MapPoint, List<Entity>> parseEntityMap() throws IOException{
		HashMap<MapPoint, List<Entity>> aoes = new HashMap<MapPoint, List<Entity>>();
		
		String line = new String();
		while (in.ready()){
			line = in.readLine().trim();
			//System.out.println(line);
			if (line.equals("END")){
				break;
			}
			String id = in.readLine().trim();
			if (line.equals("Entity")){
				ArrayList<String> tokens = lineParser.parseStringOnSemicolon(id);
				ArrayList<String> p = lineParser.parseStringOnCommaAndRemoveParen(tokens.remove(0));
				MapPoint point = new MapPoint(Integer.parseInt(p.get(0)), Integer.parseInt(p.get(1)));
				
				ArrayList<Entity> its = new ArrayList<Entity>();
				for (String name : tokens){
					its.add(EntityLibrary.getInstance().getEntity(name));
				}
				
				aoes.put(point, its);
			}
		}
		return aoes;
	}
}
